﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using LB.Common;
using LB.Server.Common;

namespace LB.Server.Core
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class BookService : IBookService
    {
        private readonly IServerManager serverManager;
        private readonly IUserManagement userManagement;

        private static readonly ApplicationLog Log = new ApplicationLog();

        public BookService(IServerManager serverManager, IUserManagement userManagement)
        {
            this.serverManager = serverManager;
            this.userManagement = userManagement;
        }

        public BookService(){}

        public List<Book> GetBooks(LoginToken loginToken)
        {
            Log.WriteInfo("Get Books invoked");
            if (!userManagement.IsSessionActive(loginToken.SessionId))
            {
                Log.WriteError("Session {0} is not active", loginToken.SessionId);
                loginToken = null;
            }

            return loginToken != null ? serverManager.GetBoks(loginToken.User.UserId) : serverManager.GetSharedBoks();
        }

        public Book GetBookById(int bookId, LoginToken loginToken)
        {
            Log.WriteInfo("Get Book By Id id = {0}", bookId);
            return ValidateLoginToken(loginToken) ? serverManager.GetBookById(bookId) : null;
        }

        public List<Book> GetBooksByAuthor(int authorId, LoginToken loginToken)
        {
            Log.WriteInfo("Get Books By Author invoked authorId = {0}", authorId);
            return ValidateLoginToken(loginToken) ? serverManager.GetbookByAuthorId(authorId) : new List<Book>();
        }

        public Book AddBook(Book newBook, LoginToken loginToken)
        {
            Log.WriteInfo("Add Book invoked, id = {0}", newBook.AuthorId);
            return ValidateLoginToken(loginToken) ? serverManager.AddBook(newBook) : null;
        }

        public List<Chapter> GetChapters(int bookId, LoginToken loginToken)
        {
            Log.WriteInfo("Get Chapter invoked");
            return ValidateLoginToken(loginToken)? serverManager.GetChapters(bookId) : new List<Chapter>();
        }

        public Chapter AddChapter(Chapter chapter, LoginToken loginToken)
        {
            Log.WriteInfo("Add Chapter invoked, id = {0}", chapter.ChapterId);
            return ValidateLoginToken(loginToken) ? serverManager.AddChapter(chapter) : null;
        }

        public DateTime Ping()
        {
            return DateTime.UtcNow;
        }

        private bool ValidateLoginToken(LoginToken token)
        {
            if (token == null)
            {
                Log.WriteError("Login token is NULL");
                return false;
            }

            if (!userManagement.IsSessionActive(token.SessionId))
            {
                Log.WriteError("Session {0} is not active", token.SessionId);
                return false;
            }
            return true;
        }
    }
}